import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/userStore'

const routes = [
    {
        path: '/',
        component: () => import('@/views/Layout/index.vue'),
        children: [
            {
                path: '',
                component: () => import('@/views/Home/index.vue')
            },
            {
                path: 'product',
                component: () => import('@/views/Product/index.vue')
            },
            {
                path: 'purchase',
                component: () => import('@/views/Purchase/index.vue')
            },
            {
                path: 'knowledge',
                component: () => import('@/views/Knowledge/index.vue')
            },
            {
                path: 'guide',
                component: () => import('@/views/Guide/index.vue')
            },
            {
                path: 'cart',
                component: () => import('@/views/CartList/index.vue')
            },
            {
                path: 'productDetail/:id',
                component: () => import('@/views/Detail/Product.vue')
            },
            {
                path: 'purchaseDetail/:id',
                component: () => import('@/views/Detail/Purchase.vue')
            },
            {
                path: 'knowledgeDetail/:id',
                component: () => import('@/views/Detail/Knowledge.vue')
            },
            {
                path: 'ask/:id',
                component: () => import('@/views/Ask/index.vue')
            },
            // {
            //     path: 'question/:id',
            //     component: () => import('@/views/Question/index.vue')
            // },
            {
                path: 'search/product',
                component: () => import('@/views/Search/Product.vue')
            },
            {
                path: 'search/Purchase',
                component: () => import('@/views/Search/Purchase.vue')
            },
            {
                path: 'search/Knowledge',
                component: () => import('@/views/Search/Knowledge.vue')
            },
            {
                path: 'order',
                name: 'Order',
                component: () => import('@/views/Order/index.vue')
            },
            {
                path: 'finance',
                component: () => import('@/views/Finance/index.vue')
            },
            {
                path: 'financeDetail/:id',
                component: () => import('@/views/Detail/Finance.vue')
            }
        ]
    },
    {
        path: '/login',
        component: () => import('@/views/Login/index.vue')
    },
    {
        path: '/forgetPwd',
        component: () => import('@/views/Pwd/ForgetPwd.vue')
    },
    {
        path: '/resetPwd',
        component: () => import('@/views/Pwd/ResetPwd.vue')
    },
    {
        path: '/editor',
        component: () => import('@/views/Editor/index.vue')
    },
    {
        path: '/userCenter',
        component: () => import('@/views/UserCenter/index.vue'),
        children: [
            {
                path: 'userInfo',
                component: () => import('@/views/UserCenter/views/UserInfo.vue')
            },
            {
                path: 'order',
                component: () => import('@/views/UserCenter/views/Order.vue')
            },
            {
                path: 'address',
                component: () => import('@/views/UserCenter/views/Address.vue')
            }, 
            {
                path: 'alterPwd',
                component: () => import('@/views/UserCenter/views/AlterPwd.vue')
            },
            {
                path: 'expertInfo',
                component: () => import('@/views/UserCenter/views/ExpertInfo.vue')
            },
            {
                path: 'knowledge',
                component: () => import('@/views/UserCenter/views/Knowledge.vue')
            },
            {
                path: 'question',
                component: () => import('@/views/UserCenter/views/Question.vue')
            },
            {
                path: 'answer',
                component: () => import('@/views/UserCenter/views/Answer.vue')
            },
            {
                path: 'purchase',
                component: () => import('@/views/UserCenter/views/Purchase.vue')
            },
            {
                path: 'productManage',
                component: () => import('@/views/UserCenter/views/ProdManage.vue')
            },
            {
                path: 'orderManage',
                component: () => import('@/views/UserCenter/views/OrderManage.vue')
            },
            { 
                path: 'finance',
                component: () => import('@/views/UserCenter/views/Finance.vue')
            }
            // {
            //     path: 'remark',
            //     component: () => import('@/views/UserCenter/views/Remark.vue')
            // }
        ]
    },
    {
        path: '/:pathMatch(.*)*',
        component: () => import('@/views/NotFound/404.vue')
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes,
})

router.beforeEach((to, from, next) => {
    const userStore = useUserStore()
    // console.log(to.fullPath, /^userCenter+/.test(to.fullPath));
    if (/^\/userCenter+\//.test(to.fullPath) && !userStore.token) {
        next({ path: '/login' })
    } 
    else {
        next()
    }
})

export default router